#pragma once
#include "DHeightfieldData.h"

dReal HeightfieldGetHeight_Callback( void* p_user_data, int x, int z )
{
	Skill::Ode::DIUnmanagedPointer^ p = Skill::Ode::DRegisteredObjects::Get(IntPtr(p_user_data));
	if(p)
	{
		Skill::Ode::Geometries::DHeightfieldData^ d = dynamic_cast<Skill::Ode::Geometries::DHeightfieldData^>(p);
		if(d && d->_Callback != nullptr)
			return d->_Callback(d->_UserData,x,z);
	}
	return System::Single::MinValue;
}

namespace Skill
{
	namespace Ode
	{		
		namespace Geometries
		{
			void DHeightfieldData::CollectManagedMemory()
			{
				_Callback = nullptr;
				_UserData = nullptr;
			}

			DHeightfieldData::DHeightfieldData(Object^ userData, DHeightfieldGetHeight^ callback,
					dReal width, dReal depth, int widthSamples, int depthSamples,
					dReal scale, dReal offset, dReal thickness, bool wrap )
				{
					_UserData = userData;
					_Callback = callback;
					_HeightData = nullptr;
					_SetPointer(dGeomHeightfieldDataCreate(),true);
					dGeomHeightfieldDataBuildCallback(_This,_This,HeightfieldGetHeight_Callback,width,depth,widthSamples,depthSamples,scale,offset,thickness,wrap? 1:0);

					_Length = widthSamples * depthSamples;
					_Width = width;
					_Depth = depth;
					_WidthSamples = widthSamples;
					_DepthSamples = depthSamples;
					_Scale = scale;
					_Offset = offset;
					_Thickness = thickness;
					_Wrap = wrap? 1:0;

					_MinHeight = System::Single::MinValue;
					_MaxHeight = System::Single::MaxValue;
				}

			int DHeightfieldData::Length::get(){return _Length;}
			dReal DHeightfieldData::Width::get(){return _Width;}
			dReal DHeightfieldData::Depth::get(){return _Depth;}
			int DHeightfieldData::WidthSamples::get(){return _WidthSamples;}
			int DHeightfieldData::DepthSamples::get(){return _DepthSamples;}
			dReal DHeightfieldData::Scale::get(){return _Scale;}
			dReal DHeightfieldData::Offset::get(){return _Offset;}
			dReal DHeightfieldData::Thickness::get(){return _Thickness;}
			bool DHeightfieldData::Wrap::get(){return _Wrap;}
			dReal DHeightfieldData::MinHeight::get(){return _MinHeight;}
			dReal DHeightfieldData::MaxHeight::get(){return _MaxHeight;}


		}
	}
}